﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Collections;
using System.Data;

namespace DAO
{
    public class KhachHangDAO : DataAccess
    {
        public static ArrayList LayDanhSach()
        {
            ArrayList arr = new ArrayList();
            string sql;
            sql = "Select * From KhachHang WHERE TrangThai = 1 ";
            DataTable dt = DataAccess.ExcuQuery(sql);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                KhachHangDTO dv = new KhachHangDTO();
                dv.MaKhachHang = Convert.ToString(dt.Rows[i]["MaKhachHang"]);
                dv.HoTenKhachHang = Convert.ToString(dt.Rows[i]["HoTenKhachHang"].ToString());
                dv.DienThoai = Convert.ToString(dt.Rows[i]["DienThoai"].ToString());
                dv.DiaChi = Convert.ToString(dt.Rows[i]["DiaChi"].ToString());
                dv.Email = Convert.ToString(dt.Rows[i]["Email"]);
                dv.TienNo = float.Parse(dt.Rows[i]["TienNoKhachHang"].ToString());
                dv.TrangThai = Convert.ToInt32(dt.Rows[i]["TrangThai"]);
                arr.Add(dv);
            }
            return arr;
        }

        public static KhachHangDTO LayThongTinKhachHang(String maKH)
        {
            KhachHangDTO kh = new KhachHangDTO();
            string sql = "Select * From KhachHang Where MaKhachHang = '" + maKH + "' ";
            DataTable dt = DataAccess.ExcuQuery(sql);

            if (dt != null && dt.Rows.Count > 0)
            {
                kh.DiaChi = Convert.ToString(dt.Rows[0]["DiaChi"]);
                kh.DienThoai = Convert.ToString(dt.Rows[0]["DienThoai"]);
                kh.Email = Convert.ToString(dt.Rows[0]["Email"]);
                kh.HoTenKhachHang = Convert.ToString(dt.Rows[0]["HoTenKhachHang"].ToString());
                kh.MaKhachHang = Convert.ToString(dt.Rows[0]["MaKhachHang"]);
                kh.TienNo = float.Parse(dt.Rows[0]["TienNoKhachHang"].ToString());
                kh.TrangThai = Convert.ToInt32(dt.Rows[0]["TrangThai"]);
            }
            return kh;
        }

        public static int CapNhatSoTienNo(String makh, float sotienno)
        {
            string sql;
            sql = "Update KhachHang Set TienNoKhachHang = " + sotienno + " where MaKhachHang = '" + makh + "' ";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }
        public static int ThemKH(KhachHangDTO kh)
        {
            string sql;
            sql = "Insert into KhachHang(MaKhachHang,HoTenKhachHang,DiaChi,DienThoai,Email,TienNoKhachHang,TrangThai) values (";
            sql += "'" + kh.MaKhachHang + "',";
            sql += "'" + kh.HoTenKhachHang + "',";
            sql += "'" + kh.DiaChi + "',";
            sql += "'" + kh.DienThoai + "',";
            sql += "'" + kh.Email + "',";
            sql += "'" + 0 + "',";
            sql += "'" + 1 + "')";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }
        public static int XoaKH(KhachHangDTO kh)
        {
            string sql;
            sql = "Update KhachHang Set TrangThai=" + 0 + "";
            sql += " Where MaKhachHang = '" + kh.MaKhachHang + "'";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }
        public static int CapNhatKH(KhachHangDTO kh)
        {
            string sql;
            sql = "Update KhachHang Set HoTenKhachHang = '" + kh.HoTenKhachHang + "',";
            sql += "DiaChi= '" + kh.DiaChi + "',";
            sql += "DienThoai='" + kh.DienThoai + "',";
            sql += "Email='" + kh.Email + "'";
            sql += " Where MaKhachHang= '" + kh.MaKhachHang + "' ";
            int kq = DataAccess.ExcuNonQuery(sql);
            return kq;
        }
    }
}
